package cn.itcast.service;

import cn.itcast.dao.UserMapper;
import cn.itcast.domain.User;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;


    /**
     * 根据用户名查询用户
     *
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {

        User user = userMapper.findUserByUsername(username);
        if (user == null) {
            return null;
        }
        return user;
    }

    /**
     * 添加用户
     *
     * @param user
     */
    @Override
    public void insertUser(User user, Integer[] roleIds) {

        //加密密码
        String password = user.getPassword();
        //调用bCryptPasswordEncoder 方法加密密码
        String encode = bCryptPasswordEncoder.encode(password);
        user.setPassword(encode);

        userMapper.insertUser(user);
        int userId = user.getId();
        this.setRoleAndUser(userId,roleIds);
    }

    /**
     * 分页查询 条件查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findUserByPage(QueryPageBean queryPageBean) {

        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);

        Page<User> page = (Page<User>) userMapper.findUserByPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除权限
     *
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        //判断是否已经关联 先查询
        //先删除中间表的power
        userMapper.deleteRoleUserId(id);
        userMapper.deleteUserId(id);

    }

    /**
     * 单个查询权限
     *
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {

        return userMapper.findUserById(id);
    }

    /**
     * 修改用户
     *
     * @param user
     */
    @Override
    public void updateUser(User user, Integer[] roleIds) {


        //删除和重建
        Integer id = user.getId();
        userMapper.deleteRoleUserId(id);
        //修改
        userMapper.updateUser(user);
         //重新建立联系
        this.setRoleAndUser(id, roleIds);

    }

    /**
     * 查询所有的角色
     * @param id
     * @return
     */
    @Override
    public List<Integer> findRoleIds(Integer id) {
        return userMapper.findRoleIds(id);
    }


    /*
     * 建立用户和角色的联系
     *
     * @param UserId
     * @param roleId
     */
    public void setRoleAndUser(Integer userId, Integer[] roleIds) {
        List<Map<String, Object>> list = new ArrayList();
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map<String, Object> map = new HashMap<>();
                map.put("userId", userId);
                map.put("roleId", roleId);
                list.add(map);
            }
            //只有当checkItemIds不为空时,才会执行这个方法
            userMapper.setRoleAndUser(list);
        }
    }
}
